/**
 * 渠道价格维护
 * @author shixc
 * @date 2018/02/01
 */
define(function () {
    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_gwdc = {},
        cols = [[
            {type: 'checkbox', fixed: 'left'},
            {field: 'GWDB001', width: 160, title: '申请单号', fixed: 'left', sort: true,
                search: 'default', searchOpts:{icon:true}}
            , {
                field: 'GWDBACTI',
                width: 80,
                title: '是否审核',
                search: 'default',
                searchOpts: {type: 'select', default: ''},
                filterDatas: app.datas_('crm_gwcd_gwcdacti'), fixed: 'left'
            }, {field: 'GWDB004', width: 160, title: '申请日期', sort: true}
            , {field: 'GWDB007_NAME', width: 160, title: '申请人员', sort: true}
            , {field: 'GWDB008_NAME', width: 160, title: '申请部门'}
            , {field: 'GWDB009_NAME', width: 160, title: '审核人'}
            , {field: 'GWDB010', width: 160, title: '审核时间', sort: true}
            , {field: 'GWDB005', width: 160, title: '生效日期', sort: true}
            // , {field: 'GWDB006', width: 160, title: '截止日期'}
            , {
                field: 'GWDB003',
                width: 160,
                title: '对象类型',
                search: 'default',
                searchOpts: {type: 'select', default: ''},
                filterDatas: app.datas_('crm_gwcd_gwcd007').concat({code: '', value: '全部'})
            }
            , {field: 'GWDB011', width: 160, title: '备注'}
            //, {field: 'GWDB012_1', title: '渠道', hide: true, search: 'default', searchOpts: {icon: true}}
            //, {field: 'GWDB012_2', title: '渠道商', hide: true, search: 'default', searchOpts: {icon: true}}
            //, {field: 'GWDB014', title: '产品编号', hide: true, search: 'default', searchOpts: {icon: true}}
            , {
                width: 60,
                title: '',
                toolbar: '#script-gwdc002setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwdc002SettingAction
    ;

    var Gwdc002 = app.Module.extend({
        init: function () {
            self = this;
            var searchArr = app.cloneJSON(cols[0]);
            self._super({
                _tag: 'gwdc002',
                _searchs: [{
                    id: 'gwdc002', tableId: 'gwdc002_table', datas: searchArr.pushArray([
                        {field: 'GWDB012_1', title: '渠道',  search: 'default', searchOpts: {icon: true}},
                        {field: 'GWDB012_2', title: '渠道商', search: 'default', searchOpts: {icon: true}},
                        {field: 'GWDB014', title: '产品编号', search: 'default', searchOpts: {icon: true}}])
                }],
                _loadTplsFull: [{
                    dom: 'gwdc002_edit', tpl: 'gwdc002_edit'
                }, {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwdc002_table'),
                    url: g_ctxPath + 'crm/gwdc002/getList',
                    exportsUrl: g_ctxPath + 'crm/gwdc002/exportsData',
                    exportsName: '渠道价格数据',
                    importsUrl: g_ctxPath + 'crm/gwdc002/importsData',
                    exportsMbUrl: g_ctxPath + 'files/渠道价格维护.xlsx',
                    title: '渠道价格信息表',
                    id: 'gwdc002_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports_import'],
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_gwdc,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwdc002setting-action').innerHTML).render({}, function (string) {
                    gwdc002SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwdc002_table)', function (obj) {

                    layer.tips(gwdc002SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if (obj.data.GWDBACTI != 'N') {
                                            app.showWarning("只有状态为N：未审核的渠道价格才可以编辑！");
                                            return;
                                        }
                                        self._openTplFull({
                                            tag: 'gwdc002_edit',
                                            tagData: {
                                                obj: obj,
                                                detail: false
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: '渠道价格信息编辑',
                                                maxmin: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwdc002_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwdc002_edit',
                        tagData: {
                            obj: obj,
                            detail: true
                        },
                        index: 0,
                        openOpts: {
                            title: '渠道价格信息编辑',
                            maxmin: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwdc002_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwdc002_edit',
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: '渠道价格信息编辑',
                                    maxmin: true
                                }
                            });
                            break;
                        case 'del':
                            app.confirm({
                                msg: '是否删除选择的数据？',
                                ok: function () {
                                    self.delDataBatch();
                                }
                            });
                            break;
                        case 'check':
                            app.confirm({
                                msg: '是否审核选择的数据？',
                                ok: function () {
                                    self.check();
                                }
                            });
                            break;
                        case 'unCheck':
                            app.confirm({
                                msg: '是否取消审核选择的数据？',
                                ok: function () {
                                    self.unCheck();
                                }
                            });
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwdc002_table', where: table_gwdc});
        },

        delData: function (obj) {
            var flag = true;
            if (obj.data.GWDBACTI == 'Y') {
                flag = false;
                app.showError('已审核的渠道价格不允许删除!');
                return;
            }
            if (flag) {
                app.confirm({
                    msg: '是否删除选择的信息？',
                    ok: function () {
                        app.ajax({
                            xurl: 'crm/gwdc002/del',
                            xdata: obj.data,
                            loading: true,
                            okmsg: '渠道价格删除成功',
                            cb: function (result) {
                                if (result.ok) {
                                    self.reloadTableCurr();
                                }
                            }
                        });
                    }
                });
            }
        },

        delDataBatch: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwdc002_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if (v.GWDBACTI == 'Y') {
                        app.showWarning("已审核的渠道价格不允许删除！");
                        flag = false;
                        return;
                    } else {
                        var _temp = app.fields_('GWDB');
                        _temp.GWDBENT = v.GWDBENT;
                        _temp.GWDB001 = v.GWDB001;
                        _xdata.push(_temp);
                    }
                });
                if (flag) {
                    app.ajax({
                        xurl: 'crm/gwdc002/deleteDataBatch',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '渠道价格信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        check: function () {
            var flag = true;
            var checkStatus = table.checkStatus('gwdc002_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWDB');
                    if (v.GWDBACTI != 'N') {
                        app.showWarning("只有状态为N：未审核的渠道价格才可以审核！");
                        flag = false;
                        return;
                    } else {
                        _temp.GWDBENT = v.GWDBENT;
                        _temp.GWDB001 = v.GWDB001;
                        _xdata.push(_temp);
                    }
                });
                if (flag) {
                    app.ajax({
                        xurl: 'crm/gwdc002/check',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '渠道价格信息审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        unCheck: function () {
            var flag = true;
            var myDate = new Date();
            var year = myDate.getFullYear();
            var month = myDate.getMonth() + 1;
            var strDate = myDate.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var time = year + '-' + month + '-' + strDate;
            var checkStatus = table.checkStatus('gwdc002_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWDB');
                    if (v.GWDBACTI != 'Y') {
                        app.showWarning("只有状态为N：未审核的渠道价格才可以取消审核！");
                        flag = false;
                        return;
                    }
                    // else if (v.GWDB005 < time) {
                    //     app.showWarning("生效日期小于当前日期，不允许取消审核！");
                    //     flag = false;
                    //     return;
                    // } 
                    else {
                        _temp.GWDBENT = v.GWDBENT;
                        _temp.GWDB001 = v.GWDB001;
                        _xdata.push(_temp);
                    }
                });
                if (flag) {
                    app.ajax({
                        xurl: 'crm/gwdc002/unCheck',
                        xdata: _xdata,
                        loading: true,
                        toJsonStr: true,
                        okmsg: '渠道价格信息取消审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }
                        }
                    });
                }
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwdc002',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field === 'GWDB001') {//申请单号开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwdb001',
                                            columnList: [
                                                {label: '申请单号', prop: 'GWDB001', width: '180'},
                                                {label: '申请日期', prop: 'GWDB004', width: '100'},
                                                {label: '申请人员', prop: 'GWDB007_NAME', width: '100'},
                                                {label: '状态', prop: 'GWDBACTI', width: '100',sortable:false}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDB001 = data.GWDB001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择申请单号',
                                        area:['600px', '470px']
                                    }
                                });
                            } else if (field === 'GWDB014') {//产品开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwam001',
                                            columnList: [
                                                {label: '产品编号', prop: 'GWAM001', width: '150'},
                                                {label: '品名', prop: 'GWAML003', width: '150'},
                                                {label: '规格', prop: 'GWAML004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDB014 = data.GWAM001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择产品',
                                        area:['600px', '470px']
                                    }
                                });
                            } else if (field === 'GWDB012_1') {//渠道开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwae001',
                                            searchParam: {
                                                hasPermission: true
                                            },
                                            columnList: [
                                                {label: '渠道编号', prop: 'GWAE001', width: '150'},
                                                {label: '说明', prop: 'GWAEL003', width: '150'},
                                                {label: '助记码', prop: 'GWAEL004', width: '150'},
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDB012_1 = data.GWAE001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择渠道',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field === 'GWDB012_2') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDB012_2 = data.GWAK001;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new Gwdc002();

});